#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib
import urllib2
import re

username = 'torri1'
password = '9021'

class HttpBot:
    """an HttpBot represents one browser session, with cookies."""
    def __init__(self):
        cookie_handler= urllib2.HTTPCookieProcessor()
        redirect_handler= urllib2.HTTPRedirectHandler()
        self._opener = urllib2.build_opener(redirect_handler, cookie_handler)

    def GET(self, url):
        return self._opener.open(url).read()

    def POST(self, url, parameters):
        return self._opener.open(url, urllib.urlencode(parameters)).read()


if __name__ == "__main__":
	bot = HttpBot()
	ignored_html = bot.POST('https://www.positronrt.com.br/rastreador/login.htm', {'j_username' : username, 'j_password' : password})
#	foo =  bot.GET('http://www.positronrt.com.br/rastreador/odometer.xhtml')
	print bot.GET('http://www.positronrt.com.br/rastreador/odometer.xhtml')
#	print bot.GET('http://www.positronrt.com.br/rastreador/monit.xhtml')
	ignored_html = bot.POST('http://www.positronrt.com.br/rastreador/logout.htm', {})
#	result = re.findall("([0-9.,]+\ km)", foo) # pega hodometro
#	result = re.findall("value=\"([0-9]{5})\">([0-9A-Z]{7})", foo) # pega placa e id
#	print result
#	exit()

#	for entry in result:
#		teste = 'http://www.positronrt.com.br/rastreador/odometer.xhtml?idvehicle=%s' % (entry[0])
#		print teste
#		bar = bot.GET(teste)
#		print bar
